//
//  GridPersonnelTopView.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/4/2.
//

import UIKit
import SnapKit

internal protocol GridPersonnelTopViewDelegate: NSObjectProtocol {
    
    /// 点击
    func gridPersonnelTopViewClick(tag :Int)
    
    func gridPersonnelTopViewTextFieldChange(text :String?)

}

class GridPersonnelTopView: UIView {
    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    internal weak var delegate: GridPersonnelTopViewDelegate?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        backgroundColor = .clear
        
        backBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(0)
            make.height.equalTo(30)
            make.width.equalTo(30)
        }
        
        addBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        seachBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(addBtn.snp.left).offset(-6)
            make.height.equalTo(30)
            make.width.equalTo(40)
        }
        
        searchTextField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(backBtn.snp.right).offset(10)
            make.height.equalTo(30)
            make.right.equalTo(seachBtn.snp.left).offset(-10)
        }
    }
    
    lazy var searchTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入关键字"
        textField.font = Theme.Font.size(14)
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.returnKeyType = .search
        textField.delegate = self
        textField.addTarget(self, action: #selector(searchDidChange(tf:)), for: .editingChanged)
        textField.layer.masksToBounds = true
        textField.layer.cornerRadius = 15
        textField.layer.borderColor = UIColor(hex6: 0x222222).cgColor
        textField.layer.borderWidth = 1.0
        //设置左侧放大镜
        let leftView = UIView()
        leftView.frame = CGRect(x: 5, y: 0, width: 35, height: 35)
        let imgV = UIImageView(image: UIImage(named: "Navigation_search"))
        imgV.frame = leftView.bounds
        imgV.contentMode = .center
        leftView.addSubview(imgV)
        
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.contentVerticalAlignment = .center
        
        addSubview(textField)
        return textField
    }()
    
    lazy var backBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        button .setImage(UIImage(named: "Navigation_Pop"), for: .normal)
        addSubview(button)
        return button
    }()
    
    lazy var seachBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("搜索", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(12, bold: true)
        button.addTarget(target, action: #selector(self.onClickButton(_:)), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 15
        addSubview(button)
        return button
    }()
    
    lazy var addBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        button .setImage(UIImage(named: "add"), for: .normal)
        addSubview(button)
        return button
    }()
    
    @objc func onClickButton(_ sender: UIButton) {
        var tag: Int = 0
        if sender == backBtn {
            tag = 1
        }else if sender == seachBtn {
            tag = 2
        }else if sender == addBtn {
            tag = 3
        }
        delegate?.gridPersonnelTopViewClick(tag: tag)
    }
    
}

extension GridPersonnelTopView: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("点击搜索")
        return true
    }
    
    @objc func searchDidChange(tf: UITextField) {
        delegate?.gridPersonnelTopViewTextFieldChange(text: tf.text)
    }
}
